﻿#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"
#include "SList.h"


//初始化通讯录

//添加通讯录数据
void AddContact(contact** con) {
	peoinfo info;
	printf("请输入联系人姓名:\n");
	scanf("%s", info.name);
	printf("请输入联系人性别:\n");
	scanf("%s", info.sex);
	printf("请输入联系人年龄:\n");
	scanf("%d", &info.age);
	printf("请输入联系人电话:\n");
	scanf("%s", info.tel);
	printf("请输入联系人住址:\n");
	scanf("%s", info.adr);
	SLTPushBack(con,info);
	printf("插入成功。\n");
}

contact* FindByName(contact* con, char name[]) {
	contact* pcur = con;
	while (pcur)
	{
		if (strcmp(pcur->data.name, name) == 0) {
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}


void ConPrint(contact* con){
	printf("%-10s %-8s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "电话", "住址");
	contact* pcur=con;
	while(pcur) {
		printf("%-10s %-8s %-4d %15s %-20s\n", pcur->data.name, pcur->data.sex, pcur->data.age, pcur->data.tel, pcur->data.adr);
		pcur = pcur->next;
	}
}


//删除通讯录数据
void DelContact(contact** con) {
	char name[NAME_MAX];
	printf("请输入要修改的联系人的姓名：\n");
	scanf("%s", &name);
	contact* head = *con;
	contact* pos = FindByName(head, name);
	if (pos == NULL) {
		printf("未查找到该联系人。\n");
		return;
	}
	free(pos);
	pos = NULL;
	printf("删除成功！\n");
}

//查找通讯录数据
void FindContact(contact* con) {
	char name[NAME_MAX];
	printf("请输入要查找的联系人的姓名：\n");
	scanf("%s", &name);
	contact* pos = FindByName(con, name);
	if (pos == NULL) {
		printf("未查找到该联系人。\n");
		return;
	}
	printf("查找成功！\n");
	printf("%-10s %-8s %-4d %15s %-20s\n", pos->data.name, pos->data.sex, pos->data.age, pos->data.tel, pos->data.adr);

}
////修改通讯录数据
void ModifyContact(contact** con) {
	char name[NAME_MAX];
	printf("请输入要修改的联系人的姓名：\n");
	scanf("%s", &name);
	contact* head = *con;
	contact* pos = FindByName(head, name);
	if (pos == NULL) {
		printf("未查找到该联系人。\n");
		return;
	}
	printf("请输入新联系人姓名:\n");
	scanf("%s", pos->data.name);
	printf("请输入新联系人性别:\n");
	scanf("%s", pos->data.sex);
	printf("请输入新联系人年龄:\n");
	scanf("%d", &pos->data.age);
	printf("请输入新联系人电话:\n");
	scanf("%s", pos->data.tel);
	printf("请输入新联系人住址:\n");
	scanf("%s", pos->data.adr);
	printf("修改成功！\n");
}
////销毁通讯录数据
void DestroyContact(contact** con) {
	SListDesTroy(con);
}